package mybatis.test;

import mybatis.dao.ExamDao;
import mybatis.pojo.ExamResult;
import mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class TestExam {
    @Test
    public void find() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        List<ExamResult> all = mapper.findAll();
        for (ExamResult examResult : all) {
            System.out.println(examResult);
        }

        MybatisUtil.close(s);
    }

    @Test
    public void findAll() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        List<ExamResult> list = mapper.findAll2();
        list.forEach(System.out::println);
        MybatisUtil.close(s);
    }

    /**
     * 改
     */

    @Test
    public void upadteExam() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        int update = mapper.update(new ExamResult(32, Date.valueOf("2021-06-06"), 85, 5, 6));
        System.out.println(update == 1 ? "修改成功" : "修改失败");
        MybatisUtil.close(s);
    }

    /**
     * .
     * 增
     */
    @Test
    public void addExam() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        int add = mapper.add(new ExamResult(new java.util.Date(), 89, 6, 1));
        System.out.println(add == 1 ? "增加成功" : "增加失败");
        MybatisUtil.close(s);
    }

    /**
     * 删
     */
    @Test
    public void deleteExame() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        int i = mapper.delete(32);
        System.out.println(i == 1 ? "删除成功" : "删除失败");
    }
@Test
    public void add2() {
        SqlSession s = MybatisUtil.get();
        ExamDao mapper = s.getMapper(ExamDao.class);
        HashMap<String, Object> map = new HashMap<>();
        map.put("examDate", new java.util.Date());
        map.put("score", 90);
        map.put("stId", 6);
        map.put("sbId", 1);
        mapper.add2(map);
        MybatisUtil.close(s);
    }


}